package com.seo.utils;

import java.util.concurrent.*;

/**
 * @author mahaibin
 * @desc    线程池工具类
 */
public class ThreadPoolUtils {

    private static ThreadPoolExecutor threadPoolExecutor;

    /**
     * 无返回值
     * @param runnable
     */
    public static void execute(Runnable runnable) {
        getThreadPool().execute(runnable);
    }

    /**
     * 有返回值
     * @param callable
     * @param <T>
     * @return
     */
    public static <T> Future<T> submit(Callable<T> callable) {
        return getThreadPool().submit(callable);
    }

    private static ThreadPoolExecutor getThreadPool() {
        if (threadPoolExecutor != null) {
            return threadPoolExecutor;
        } else {
            synchronized (ThreadPoolUtils.class) {
                if (threadPoolExecutor == null) {
                    int coreSize = Runtime.getRuntime().availableProcessors();
                    threadPoolExecutor = new ThreadPoolExecutor(coreSize, 32, 60, TimeUnit.SECONDS,
                            new LinkedBlockingQueue<>(64), new ThreadPoolExecutor.CallerRunsPolicy());
                }
                return threadPoolExecutor;
            }
        }

    }
}
